package cn.fotoit.product.system.controller;

import cn.fotoit.product.system.service.IoService;
import cn.fotoit.product.system.service.UserLogService;
import cn.fotoit.product.util.ReturnBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "日志接口", tags = "日志相关接口，日志添加，修改，删除，查询")
@RestController
@CrossOrigin
@RequestMapping("userLog")
public class UserLogController {

    @Autowired
    private UserLogService userLogService;


    @PostMapping("deleteById")
    public ReturnBean deleteById(@RequestParam Integer id) {
        userLogService.deleteById(id);
        return ReturnBean.success("删除成功");
    }

    @PostMapping("findAll")
    public ReturnBean findAll(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size) {
        return ReturnBean.success("查询成功", userLogService.findAll(page, size));
    }

    @PostMapping("findById")
    public ReturnBean findById(
            @RequestParam Integer id) {
        return ReturnBean.success("查询成功", userLogService.findById(id));
    }

    @PostMapping("findByType")
    public ReturnBean findByType(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size,
            @RequestParam Integer type) {
        return ReturnBean.success("查询成功", userLogService.findByType(type, page, size));
    }

    @PostMapping("findByUserId")
    public ReturnBean findByUserId(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size,
            @RequestParam Integer userId) {
        return ReturnBean.success("查询成功", userLogService.findByUserId(userId, page, size));
    }

    @PostMapping("findByUserIdAndType")
    public ReturnBean findByUserIdAndType(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size,
            @RequestParam Integer userId,
            @RequestParam Integer type) {
        return ReturnBean.success("查询成功", userLogService.findByUserIdAndType(userId, type, page, size));
    }


    @Autowired
    private IoService ioService;


    @ApiOperation(value = "初始化数据库 需要密码")
    @GetMapping("io")
    public ReturnBean ioService(
            String pwd) throws IllegalAccessException {
        if (pwd.equals("fotoit@2018")) {
            ioService.addPermissions();
        }
        return ReturnBean.success("查询成功");
    }

}
